package com.openx.xoj.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.openx.xoj.mapper.QuestionFavourMapper;
import com.openx.xoj.mapper.QuestionMapper;
import com.openx.xoj.model.entity.Question;
import com.openx.xoj.model.entity.QuestionFavour;
import com.openx.xoj.model.vo.QuestionVO;
import com.openx.xoj.service.QuestionFavourService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;


@Service
public class QuestionFavourServiceImpl extends ServiceImpl<QuestionFavourMapper, QuestionFavour>
        implements QuestionFavourService {

    @Resource
    private QuestionFavourMapper questionFavourMapper;
    @Resource
    private QuestionMapper questionMapper;


    @Override
    public List<QuestionVO> getFavourQuestionList(long userId) {
        List<QuestionFavour> questionFavours = questionFavourMapper.selectList(new QueryWrapper<QuestionFavour>().eq("userId", userId));
        if (!questionFavours.isEmpty()) {
            List<Long> questionIds = questionFavours.stream().map(QuestionFavour::getQuestionId).collect(Collectors.toList());
            List<Question> questions = questionMapper.selectList(new QueryWrapper<Question>().in("id", questionIds));
            List<QuestionVO> questionVOS = new ArrayList<>();
            for (Question question : questions) {
                questionVOS.add(QuestionVO.objToVo(question));
            }
            return questionVOS;
        }
        return null;
    }
}




